package com.monkey.tensquare.search.controller;

import com.monkey.tensquare.search.entity.Article;
import com.monkey.tensquare.search.service.ArticleService;
import entity.PageResult;
import entity.Result;
import entity.StateCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    // 添加文章
    @PostMapping
    public Result add(@RequestBody Article article) {
        try {
            articleService.add(article);
            return new Result(StateCode.OK, true, "添加成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StateCode.ERROR, false, "添加失败", null);
        }
    }

    // 关键字查询带分页
    @GetMapping("/{kw}/{page}/{size}")
    public Result search(@PathVariable String kw, @PathVariable Integer page, @PathVariable Integer size) {
        try {
            PageResult<Article> articles = articleService.search(kw, page, size);
            return new Result(StateCode.OK, true, "查询成功！", articles);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StateCode.OK, true, "查询失败！", null);
        }
    }
}
